<?php
/**
* $Id: tv_Model.php 2942 2009-11-30 10:48:18Z huong.pham $
* @desc Class Control Extend model class for manipulate with result data easier
* 
* @author Nguyen Le, Huong Pham
*/

class TV_Model extends CI_Model {

    const QUERY_GET = 'get';
    const QUERY_FIND = 'find';
    
    protected $selected_columns = '';
    protected $table = null;

    function TV_Model() {
        parent::__construct();
        
        //Multi database. Add database config in config/database.php before used it.
            $this->load->database();
    }
    
    function getRules($scenario){
        $rules = $this->rules();
        return $rules[$scenario];
    }
    
    //abstract function selectDefault();

    function selectAll(){
        $this->selected_columns = '*';
        return $this;    
    }
    
    function prefixSelectedColumn(){
        if ($this->selected_columns == '*') 
            $prefixColumn = $this->table.'.*';
        else
            $prefixColumn = $this->table . '.' . str_replace(', ',', '.$this->table.'.', $this->selected_columns);
        return $prefixColumn;
    }
    
    /**
    * query data and return array of returned rows
    * 
    * @param string $sql
    * @param array $params values to bind into sql parameters
    * 
    * @param string $key name of column that value will be used as key
    * if set then return array is an associative array
    * 
    * @param array $callback function exectued to process returned data before any actual usage
    */
    function query_array($sql, $params = array(), $key = null, $callback = null){
        $resource = $this->db->query($sql, $params);
        if (!empty($resource) && $resource->num_rows>0)
        {
            if (is_null($key) && is_null($callback))
            {
                return $resource->result_array();
            }
            else
            {
                //Code is copied from DB_result->result_array() as this library cannot be overrided
                $resource->_data_seek(0);
                while ($row = $resource->_fetch_assoc())
                {
                    if (is_array($callback))
                    {
                        $row = call_user_func($callback, $row);
                    }
                    if (is_null($key))
                    {
                        $resource->result_array[] = $row;
                    }
                    else
                    {
                        $resource->result_array[$row[$key]] = $row;
                    }
                }
                
                return $resource->result_array;
            }
        }
        else
            return null;
    }
    
    /**
    * Query data and get only one first matched row
    * 
    * @param mixed $sql
    * @param mixed $params
    */
    function query_row($sql, $params){
        $sql = str_replace("\n",' ',$sql);
        //Try to limit the result set
        if (strpos(strtolower($sql), ' limit ') === false && strpos(strtolower($sql), " union ") === false){
            //SQL does not have LIMIT clause, add it in!!!
            $sql .= ' LIMIT 1';
        }
        
        $resource = $this->db->query($sql, $params);
        if (!empty($resource)){
            return $resource->row_array();
        }
        else
            return null;    
    }    

}